package com.qf.smsplatform.webmaster.mq;

import com.qf.smsplatform.common.constants.CacheConstants;
import com.qf.smsplatform.common.constants.RabbitMQConstants;
import com.qf.smsplatform.common.model.StandardSubmit;
import com.qf.smsplatform.webmaster.service.ClientBusinessService;
import com.qf.smsplatform.webmaster.service.api.CacheService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;


/**
 * @Author: LeeRui
 * 2021/1/27 下午 8:30
 **/
//监听网关fee队列发送的MQ消息，进行数据库费用扣除操作
@Component
@Slf4j
public class GatewayFeeQueueListener {

    @Resource
    private CacheService cacheService;

    @Autowired
    private ClientBusinessService clientBusinessService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //监听接口发送消息队列
    @RabbitListener(queues = {RabbitMQConstants.SMS_FEE_SEND_TOPIC})
    public void consume(StandardSubmit standardSubmit, Channel channel, Message message) throws IOException {
        //获取短信发送最终状态
        int reportState = standardSubmit.getReportState();
        //获取客户ID
        Long clientID = Long.valueOf(standardSubmit.getClientID());
        //获取mobile
        String mobile = standardSubmit.getDestMobile();
        //获取短信费用
        long fee = standardSubmit.getFee();
        if (reportState == 0) { //短信发送成功
            log.info("【WebMaster-GatewayFee】  短信发送成功,开始执行业务。。。reportState={}", reportState);
            // TODO  短信发送成功,保存数据到zset供限流策略查询校验
            try {
                //准备key  LIMIT:+客户ID+":"+客户手机号
                String key = CacheConstants.CACHE_PREFIX_SMS_LIMIT + clientID + ":" + mobile;
                //准备value
                long value = System.currentTimeMillis();
                //准备member
                long member = value;
                //将数据缓存到Redis缓存
                cacheService.zadd(key, value, member);
                log.info("【WebMaster-GatewayFee】  成功保存当前时间戳到Redis供策略模块限流策略使用！！！value={}", value);
            } catch (Exception e) {
                e.printStackTrace();
                log.info("【WebMaster-GatewayFee】  Redis缓存数据存数异常，请查明原因！！！");
            }

            // TODO  短信发送成功~扣费操作  ----   将缓存中预扣除的费用扣除，并且修改数据库
            try {
                //将Redis冻结费用扣除本次费用
                cacheService.hincr(CacheConstants.CACHE_PREFIX_CUSTOMER_FEE + clientID, "fee", -fee);
                //数据库扣除费用
                clientBusinessService.feededuction(fee, clientID);
                log.info("【WebMaster-GatewayFee】  成功扣除冻结费用和数据库费用！！！fee={}", fee);
            } catch (Exception e) {
                e.printStackTrace();
                log.info("【WebMaster-GatewayFee】  费用操作异常，请查明原因！！！");
            }
        } else if (reportState == 2) {  //短信发送失败
            // TODO  短信发送失败~扣费操作  -----   将缓存中预扣除的费用添加回客户的费用缓存中
            log.info("【WebMaster-GatewayFee】  短信发送失败,开始执行托底业务。。。reportState={}", reportState);
            try {
                //将冻结费用扣除本次费用并添加回客户费用缓存
                cacheService.hincr(CacheConstants.CACHE_PREFIX_CLIENT + clientID, "fee", fee);
                cacheService.hincr(CacheConstants.CACHE_PREFIX_CUSTOMER_FEE + clientID, "fee", -fee);
                log.info("【WebMaster-GatewayFee】  成功扣除冻结费用并回滚到客户余额！！！fee={}", fee);
            } catch (Exception e) {
                e.printStackTrace();
                log.info("【WebMaster-GatewayFee】  费用操作异常，请查明原因！！！");
            }
        }
        //消费MQ消息并手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
